
#if 0
#include <stdio.h>
int main(char argc, char *argv[])
{
	int i = 1;
	while(1){
		
		printf("gateway world!!!%d\n",i);
		
		if (i < 10){
			i++;
		}else{
			i = 1;	
		}
		

		sleep(1);//一秒打印
	}
	return  0;
}

#else

#include     <stdio.h>
#include     <stdlib.h> 
#include     <unistd.h>  
#include     <sys/types.h>
#include     <sys/stat.h>
#include     <fcntl.h> 
#include     <termios.h>
#include     <errno.h>
   

#include <string.h>
#include <arpa/inet.h>

#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
//extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr,
//void *(*__start_routine) (void *), void *__arg));
#define PORT 10071
#define REMOTE_IP "183.60.111.161"
#define MAXBUF 512
char senddate=0;
char send_buf[MAXBUF+1];
char recv_buf[MAXBUF+1];

char breath[10]={"hello"};


void *thread(void *x)  
{  
 int new_fd = *((int*)x);
 int fd;
    int i;
    int len;
    int n = 0;      
    char read_buf[256];
    char write_buf[256];
    struct termios opt; 
    char count=0;
    fd = open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
    if(fd == -1)
    {
        perror("open serial 0\n");
        exit(0);
    }

    tcgetattr(fd, &opt);      
	bzero(&opt, sizeof(opt));
    
	tcflush(fd, TCIOFLUSH);

	cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);
    
    opt.c_cflag &= ~CSIZE;  
    opt.c_cflag |= CS8;   
    opt.c_cflag &= ~CSTOPB; 
    opt.c_cflag &= ~PARENB; 
    opt.c_cflag &= ~CRTSCTS;
    opt.c_cflag |= (CLOCAL | CREAD);
 
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
 
    opt.c_oflag &= ~OPOST;
    
    opt.c_cc[VTIME] = 0;
    opt.c_cc[VMIN] = 0;
    
    tcflush(fd, TCIOFLUSH);
 
    printf("configure complete\n");
	
    if(tcsetattr(fd, TCSANOW, &opt) != 0)
    {
        perror("serial error");
        return NULL; 
    }

    printf("start send and receive data\n");

	while(1)
    {    
        n = 0;
        len = 0;
        bzero(read_buf, sizeof(read_buf)); 
        bzero(write_buf, sizeof(write_buf));
 
        while( (n = read(fd, read_buf, sizeof(read_buf))) > 0 )
        {
            for(i = len; i < (len + n); i++)
            {
                write_buf[i] = read_buf[i - len];
            }
            len += n;
        }
        write_buf[len] = '\0';
              
        printf("Len %d \n", len);
        printf("%s \n", write_buf);
          if(senddate)
           {
        n = write(fd, send_buf, strlen(send_buf));
           senddate=0;
               bzero(send_buf, sizeof(send_buf)); 
           }
        printf("write %d chars\n",n);
           if(len)
         send(new_fd, read_buf, strlen(read_buf), 0); //第 4 步 向套接字中写入字符串
         else
          {
                count++;
               if(count>30)
                {
              send(new_fd, breath, strlen(breath), 0); //第 4 步 向套接字中写入字符串
                     count=0;
                      }
              }
        sleep(1);
    }



   
   // while(1)
    {
     //  bzero(send_buf, MAXBUF + 1);
      //  scanf("%s",send_buf);
     //   send(new_fd, send_buf, strlen(send_buf), 0); //第 4 步 向套接字中写入字符串
    }
    return NULL;  
} 
//////////////////////////////////////////////////////////////////////////////////////////////////////

int main()
{
    int sockfd;
    int i;
#if 1
     if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)//第 1 步 创建一个体套接字
           {
         printf("socket err:\n");
         exit(1);
          }
          else
          {
  
            printf("socket id=%d:\n",sockfd);
         }
   struct sockaddr_in svraddr;//第 2 步 设置 addr 结构体
      bzero(&svraddr,sizeof(svraddr));
   svraddr.sin_family=AF_INET;//使用 internet 协议
   svraddr.sin_port=htons(PORT);
   //inet_aton("183.60.111.161",&svraddr.sin_addr);
   svraddr.sin_addr.s_addr=inet_addr(REMOTE_IP);
     i=1;
   while(i)
      {


      
   if((connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr)))<0)
          {
            printf("err  00000errr:\n");
               //   exit(1);
              send_buf[0]='e';
              send_buf[1]='e';
             send_buf[2]='e';
              send_buf[3]='f';
             senddate=1;
               sleep(10);
             }
          else
          {

          printf("成功:\n");
           printf("connect ok:\n");
              i=0;
               }
              //第 3 步 连接服务器
              }
   pthread_t ntid;
  // pthread_create(&ntid,NULL,thread,&sockfd);

   while(1)
   {
     bzero(recv_buf, MAXBUF + 1);/* 接收对方发过来的消息，最多接收 MAXBUF 个字节 */
     int len = recv(sockfd, recv_buf, MAXBUF, 0);/* 接收服务器来的消息 */
     if(len > 0)
          {
        printf("接收消息成功:'%s'，共%d个字节的数据\n", recv_buf, len);
       if(senddate==0)
           {
               for(i=0;i<len;i++)
              send_buf[i]=recv_buf[i];

             senddate=1;
          }
           }
     else
           {
        printf("消息接收失败！错误代码是%d，错误信息是'%s'\n", errno, strerror(errno));

              send_buf[0]='e';
              send_buf[1]='e';
             send_buf[2]='e';
              send_buf[3]='e';
             senddate=1;
               sleep(3);
            }
   }
 /* 关闭连接 */
 close(sockfd);
#endif
 return 0;
}


//make package/helloworld/compile V=s
//find bin/ -name "helloworld*.ipk"
//假设在package/network/zigbeeservice_test目录下是要编译的代码。
//先make menuconfig
//于pthread库不是Linux系统默认的库，连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时，在编译中要加-lpthread参数:
//找到对应的选项，然后配置该选项为'M'，保存退出
//然后输入make package/network/zigbeeservice_test/compile即可编译出ipk文件。
//mipsel-openwrt-linux-gcc gateway.c  -ldl -lpthread  -o gateway
//mipsel-openwrt-linux-gcc lc_tty.c lc_tty.h main.c -ldl -lpthread -o main


int  maincom(void)
{
    int fd;
    int i;
    int len;
    int n = 0;      
    char read_buf[256];
    char write_buf[256];
    struct termios opt; 
    
    fd = open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
    if(fd == -1)
    {
        perror("open serial 0\n");
        exit(0);
    }

    tcgetattr(fd, &opt);      
	bzero(&opt, sizeof(opt));
    
	tcflush(fd, TCIOFLUSH);

	cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);
    
    opt.c_cflag &= ~CSIZE;  
    opt.c_cflag |= CS8;   
    opt.c_cflag &= ~CSTOPB; 
    opt.c_cflag &= ~PARENB; 
    opt.c_cflag &= ~CRTSCTS;
    opt.c_cflag |= (CLOCAL | CREAD);
 
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
 
    opt.c_oflag &= ~OPOST;
    
    opt.c_cc[VTIME] = 0;
    opt.c_cc[VMIN] = 0;
    
    tcflush(fd, TCIOFLUSH);
 
    printf("configure complete\n");
	
    if(tcsetattr(fd, TCSANOW, &opt) != 0)
    {
        perror("serial error");
        return -1;
    }

    printf("start send and receive data\n");

	while(1)
    {    
        n = 0;
        len = 0;
        bzero(read_buf, sizeof(read_buf)); 
        bzero(write_buf, sizeof(write_buf));
 
        while( (n = read(fd, read_buf, sizeof(read_buf))) > 0 )
        {
            for(i = len; i < (len + n); i++)
            {
                write_buf[i] = read_buf[i - len];
            }
            len += n;
        }
        write_buf[len] = '\0';
              
        printf("Len %d \n", len);
        printf("%s \n", write_buf);
 
        n = write(fd, write_buf, len);
        printf("write %d chars\n",n);
        
        sleep(2);
    }
	
}
#endif



